# -*- coding: utf-8 -*-
# @Time : 2022/1/6 15:45
# @Author : Limusen
# @File : demo_element_excel_22


import os
import xlrd3

current_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
excel_path = os.path.join(current_path, '..', 'element_info_datas')


class DemoExcel22:

    def __init__(self, module_name, page_name, element_path=excel_path):
        """
        :param module_name: 模块名,在element_info_datas模块下
        :param page_name: element_info_datas模块下的页面名称
        :param element_path:  excel的地址
        """
        # 指定excel的地址
        self.excel_path = os.path.join(element_path, module_name, page_name)

        # 创建excel对象
        self.work_book = xlrd3.open_workbook(self.excel_path)
        # 创建sheet对象 默认取第一个 一个页面一个excel
        self.sheet = self.work_book.sheet_by_index(0)
        # 获取excel总行数
        self.row_count = self.sheet.nrows

    def get_element_demo(self):
        info_elements = {}
        for info in range(1, self.row_count):
            # 可以发现这里会将不同的行数的数打印出来
            print(self.sheet.cell_value(info,3))
        return info_elements

    def get_element_info(self):
        info_elements = {}
        for info in range(1, self.row_count):
            # 取出每行然后进行拼接处理
            element_info = {}
            element_info['element_name'] = self.sheet.cell_value(info, 1)
            element_info['locator_type'] = self.sheet.cell_value(info, 2)
            element_info['locator_value'] = self.sheet.cell_value(info, 3)
            timeout_value = self.sheet.cell_value(info, 4)
            # 如果超时时间为浮点型则直接使用timeout_value 如果为空则使用数字5
            element_info['timeout'] = timeout_value if isinstance(timeout_value, float) else lo
            info_elements[self.sheet.cell_value(info, 0)] = element_info
        return info_elements


if __name__ == '__main__':
    ex = DemoExcel22("baidu", "baidu_page.xls")
    print(ex.get_element_info())
